package com.lxq.www.enumeration.enumconverter;

import com.lxq.www.enumeration.IdentityEnum;

import javax.persistence.AttributeConverter;

public class IdentityConverter implements AttributeConverter<IdentityEnum,String> {
    @Override
    public String convertToDatabaseColumn(IdentityEnum identityEnum) {
        if (identityEnum==null){
            identityEnum=IdentityEnum.ORDINARY;
        }
        return identityEnum.getRoleStr();
    }

    @Override
    public IdentityEnum convertToEntityAttribute(String roleStr) {
        switch (roleStr){
            case "管理员":return IdentityEnum.ADMIN;
            case "普通用户":return IdentityEnum.ORDINARY;
            default:return IdentityEnum.NULL_ROLE;
        }
    }
}
